package iot.wlink.network.rule.trigger;

import iot.wlink.network.payload.up.UpPayload;
import iot.wlink.network.rule.exec.RuleActionExec;
import iot.wlink.network.rule.model.RuleAction;
import iot.wlink.network.rule.model.RuleCondition;
import iot.wlink.network.rule.model.RuleModel;
import iot.wlink.network.rule.model.TriggerPatternEm;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author : willian fu
 * @date : 2021/8/23
 * 触发器
 */
public interface Trigger {

    //缓存规则 设备UID:属性标识符 -> 拥有该属性规则的规则组
    Map<String, List<RuleModel>> rules = new ConcurrentHashMap<>();

    /**
     * 判断是否满足触发条件
     * @param pattern and / or
     * @param conditions 条件组
     * @return 满足的条件 null:都不满足
     */
    List<RuleCondition> beTriggered(TriggerPatternEm pattern, List<RuleCondition> conditions);

    /**
     * 执行动作
     * @param exec 动作执行器，可以用函数式接口
     */
    default void executeActions(RuleActionExec exec, UpPayload payload, List<RuleCondition> conditions, List<RuleAction> actions){
        exec.execute(payload, conditions, actions);
    }
}
